/*
 * Copyright(c) 2012 Donghong Inc.
 */
package com.hatic.login;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jxstar.dao.BaseDao;
import org.jxstar.dao.DaoParam;
import org.jxstar.util.MapUtil;
import org.jxstar.util.resource.JsParam;


/**
 * 单点登录辅助工具类。
 * 1、新建一张用户映射表：pm_user，字段：user_id, cas_user_id, cas_user_code, cas_user_name,  add_date
 *
 * @author TonyTan
 * @version 1.0, 2012-7-18
 */
public class PmLoginUtil {
	private static BaseDao _dao = BaseDao.getInstance();
	
	/**
	 * 获取用户登录成功的数据对象
	 * @param request
	 * @return
	 */
	public static String loginData(HttpServletRequest request) {
		Map<String,String> mpUser = (Map<String,String>) 
			request.getSession().getAttribute(JsParam.CURRUSER);
		if (mpUser == null || mpUser.isEmpty()) {
			return "";
		}
		
		PmLoginBO login = new PmLoginBO();
		return login.getScript(mpUser);
	}
	
	/**
	 * 根据集成系统的用户ID找到PM系统中的用户编码
	 * @param casUserId
	 * @return
	 */
	public static String queryUserCode(String casUserId) {
		String sql = "select user_code from sys_user where user_id in " + 
					 "(select user_id from pm_user where cas_user_id = ?)";
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(casUserId);
		Map<String,String> mp = _dao.queryMap(param);
		
		return MapUtil.getValue(mp, "user_code");
	}
	
	/**
	 * 根据PM系统中的用户ID找到集成系统的用户信息
	 * @param userId
	 * @return
	 */
	public static Map<String,String> queryCasUser(String userId) {
		String sql = "select cas_user_id, cas_user_code ,cas_user_name from pm_user where user_id = ?";
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(userId);
		Map<String,String> mp = _dao.queryMap(param);
		
		return mp;
	}
	
	/**
	 * 取用户分配信息
	 * @param assignId
	 * @return
	 */
	public static String queryAssign(String assignId) {
		String sql = "select fun_id, data_id, assign_userid from wf_assign where assign_id = ?";
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(assignId);
		Map<String,String> mp = _dao.queryMap(param);
		
		if (mp.isEmpty()) return "";
		return "{funId:'"+ mp.get("fun_id") +"', dataId:'"+ 
			mp.get("data_id") +"', userId:'"+ 
			mp.get("assign_userid") +"'}";
	}
}
